﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace GPMS.Web.Controllers
{
    using GPMS.Model;
    using GPMS.Web.Helpers;
    using GPMS.BLL;
    using System.Web.Security;
    using System.Text;
    /// <summary>
    /// 此控制器提供ajax调用并返回json
    /// </summary>
    public class JsonController : BaseController
    {
        /// <summary>
        /// 检查用户名是否重复
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <returns></returns>
        public JsonResult CheckUserIsExist(string userName)
        {
            return Json(gpmsDb.UserInfos.Any(u => u.UserName == userName));
        }

        public JsonResult GetDesktop()
        {
            return Json(LinkHelper.GetListDynamic(LinkType.DestopIcon,gpmsDb,Url,Request));
        }

        public JsonResult GetAdmin()
        {
            return Json(LinkHelper.GetListDynamic(LinkType.Admin, gpmsDb, Url, Request));
        }

        public JsonResult GetMessageNotRead()
        {
            return Json(MessageHelper.GetNotReadCount(AccountHelper.GetLoginUserID(Request), gpmsDb));
        }

        public JsonResult GetUserInfo()
        {
            //StringBuilder sb = new StringBuilder();
            //Guid userid = AccountHelper.GetLoginUserID(Request);
            //gpmsDb.Roles
            //    .Where(r => r.Users.Any(u => u.UserID == userid) && !r.IsDeleted && r.Display)
            //    .ToList().ForEach(r => sb.Append(r.RoleName + ","));
            //string role = sb.Length > 1 ? sb.ToString().Substring(0, sb.Length - 1) : string.Empty;
            //role += " " + gpmsDb.UserInfos.First(u => u.UserID == userid).UserName;
            return Json(AccountHelper.GetUserRoleStr(Request,gpmsDb));
        }

    }
}
